
function loadFriends() {
  var req = opensocial.newDataRequest();
  var viewerFriends = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });
  var ownerFriends = opensocial.newIdSpec({ "userId" : "OWNER", "groupId" : "FRIENDS" });
  var opt_params = {};
  opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
  req.add(req.newFetchPeopleRequest(viewerFriends, opt_params), 'viewerFriends');
  req.add(req.newFetchPeopleRequest(ownerFriends, opt_params), 'ownerFriends');
  req.send(onLoadFriends);
}

function onLoadFriends(data) {
  var viewerFriends = data.get('viewerFriends').getData();
  var ownerFriends = data.get('ownerFriends').getData();

  var html = [];
  html.push('<h1>Owner Friends</h1>');
  html.push('<ul>');
  if (ownerFriends) ownerFriends.each(function(person) { html.push(printPerson(person)) });
  html.push('</ul>');

  html.push('<h1>Viewer Friends</h1>');
  html.push('<ul>');
  if (viewerFriends) viewerFriends.each(function(person) { html.push(printPerson(person)) });
  html.push('</ul>');

  document.body.innerHTML = html.join('');
}

function printPerson(person) {
  return '<li>' + person.getDisplayName() + '</li>';
}

loadFriends();
